package org.ruixue.util;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public static void main(String args[]) throws ParseException {
		DateUtil.getTimeInMillis(-2);
		DateUtil.timeStamp2Date("1586155247");
		DateUtil.formatStringToDate("9999-01-01","yyyy-MM-dd");// HH:mm:ss
	}

	/**
	 * 计算两个日期之间相差的天数
	 *
	 * @param smdate
	 *            较小的时间
	 * @param bdate
	 *            较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate, Date bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 字符串的日期格式的计算
	 */
	public static int daysBetween(String smdate, String bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 获取当前时间
	 */
	public static String getCurrentDateStr() throws ParseException {
		Date date = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = df.format(date);
		return str;
	}

	/**
	 * 时间转字符串
	 */
	public static String formatDateToString(Date date, String format) throws ParseException {
		if (date == null) {
			return null;
		}
		SimpleDateFormat df = new SimpleDateFormat(format);
		String str = df.format(date);
		System.out.println(str);
		return str;
	}

	/**
	 * 时间转字符串
	 */
	public static String formatDateToString(Date date) throws Exception {
		if (date == null) {
			return null;
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = df.format(date);
		return str;
	}

	/**
	 * 时间戳转换为Date类型
	 *
	 * @param date
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static Date stampToDate(String date, String format) throws ParseException {
		if (StringUtils.isBlank(date)) {
			return null;
		}
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.parse(DateFormatUtils.format(Long.valueOf(date), format));
	}

	/**
	 * 字符串转时间
	 */
	public static Date formatStringToDate(String date, String format) throws ParseException {
		if (StringUtils.isBlank(date)) {
			return null;
		}
		SimpleDateFormat df = new SimpleDateFormat(format);
		Date str = df.parse(date);
		System.out.println(str);
		return str;
	}

	/**
	 * 字符串转时间
	 */
	public static Date formatStringToDate(String date) throws Exception {
		if (StringUtils.isBlank(date)) {
			return null;
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date str = df.parse(date);
		return str;
	}

	/**
	 * 获取当前时间戳
	 * 
	 * @return
	 */
	public static Integer getTimeInMillis(Integer month) {
		Date de = new Date();
		Calendar calendar = Calendar.getInstance();// 获取当前日期
		calendar.setTime(de);
		if (month != null) {
			calendar.add(Calendar.MONTH, month);
		}
		System.out.println(getTimeMillis(calendar.getTimeInMillis()));
		return getTimeMillis(calendar.getTimeInMillis());
	}

	/**
	 * 处理时间戳 / 1000
	 * 
	 * @param time
	 * @return
	 */
	public static Integer getTimeMillis(long time) {
		int mi = (int) (time / 1000);
		return mi;
	}

	/**
	 * 时间戳转换成日期格式字符串
	 * 
	 * @param seconds
	 *            精确到秒的字符串
	 * @param formatStr
	 * @return
	 */
	public static Date timeStamp2Date(String seconds) {
		String format = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = new Date(Long.valueOf(seconds) * 1000);
		System.out.println(sdf.format(date));
		return date;
	}
}
